package com.bwt.top.bwt_ad_flutter.ad;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.bwt.top.ad.bean.NativeAdInfo;
import com.bwt.top.ad.impl.NativeAd;
import com.bwt.top.ad.utils.AdUtil;
import com.bwt.top.bwt_ad_flutter.model.AdParams;
import com.bwt.top.bwt_ad_flutter.util.LogUtil;
import com.bwt.top.exception.AdError;

import java.util.List;

public class BwtInterstitialAd extends BwtNativeExpressAd {
    private final boolean loadThenShow;

    public BwtInterstitialAd(@NonNull AdParams adParams, @NonNull Activity activity, boolean loadThenShow) {
        super(adParams, activity);
        this.loadThenShow = loadThenShow;
    }

    @Override
    public void onAdReceive(List<NativeAdInfo> list) {
        super.onAdReceive(list);
        if (list != null && list.size() > 0 && loadThenShow) {
            showInterstitialAd();
        }
    }

    /**
     * 展示插屏广告
     */
    public void showInterstitialAd() {
        LogUtil.log("插屏广告开始尝试展示, adId: " + getAdParams().getAdId());
        NativeAdInfo nativeAdInfo = getNativeAdInfo();
        NativeAd nativeAd = getNativeAd();
        if (nativeAdInfo != null
                && nativeAd != null
                && nativeAd.getContext() != null
                && nativeAd.getContext() instanceof Activity
                && !((Activity) nativeAd.getContext()).isFinishing()
        ) {
            // 设置插屏广告是否可以按返回键关闭广告
            AdUtil.setCancelable(true);
            // 信息流广告 作为插屏形式展示
            AdUtil.showNativeAdConvenient((Activity) nativeAd.getContext(), nativeAdInfo, true, this);
        } else {
            LogUtil.log("插屏广告展示失败, adId: " + getAdParams().getAdId() + ", error: 当前情况不符合展示广告条件");
            onAdFailed(new AdError(-1, "当前情况不符合展示广告条件"));
        }
    }
}
